package com.tyf.common.aspect;

import com.tyf.common.annotation.BaseDataCache;
import com.tyf.common.redis.RedisManager;
import com.tyf.common.constant.Fetches;
import com.tyf.common.service.BaseService;
import com.tyf.common.util.ReflectUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Description: 自动处理MDM公共数据缓存
 * author:      liutao
 * Createdate:  2016年12月31日下午10:59:28
 */
@Aspect
//@Component
public class BasDataCacheAspect {
	@Autowired
	private RedisManager redisManager;
	protected static final Logger logger = LoggerFactory.getLogger(BasDataCacheAspect.class);
	String keySplit = ":";
	
	////////////////////////////////////////////////////////////////////////////////
	////定义切入点
	////////////////////////////////////////////////////////////////////////////////
	// 缓存的删除-匹配save,清除全量缓存rows
	@Pointcut(value = "execution(* com.tyf..service..**.save(..)) " +
			"|| execution(* com.euond..service..**.audit(..))")
	public void deleteRowsCachePointCut() {
	}

	// 缓存的删除-匹配delete*与update*,清除row与name的缓存
	@Pointcut(value = "(execution(* com.tyf..service..**.delete*(..)) " +
			"|| execution(* com.euond..service..**.update*(..))) && args(m,..)", argNames = "m")
	public void deleteRowAndNameCachePointCut(Object m) {
	}
	
	////////////////////////////////////////////////////////////////////////////////
	////增强实现
	////////////////////////////////////////////////////////////////////////////////
	@SuppressWarnings("rawtypes")
	@After(value = "deleteRowsCachePointCut()")
	public void deleteRowsCacheAdvice(JoinPoint jp) throws Throwable {
		// 针对未继承BaseService的
		if(!(jp.getTarget() instanceof BaseService)){
			return;
		}

		// 前置检查
		BaseService targetService = (BaseService)jp.getTarget();
		Class<?> mclazz = targetService.getEntityClass();		//获取当前传入的实体类型
		if(!mclazz.isAnnotationPresent(BaseDataCache.class)){
			return;
		}

		// 处理缓存
		String baseKey = this.getBaseKeyByEntityClass(mclazz);
		String key = baseKey + keySplit + Fetches.ROWS;
//		redisManager.remove(key + Fetches.ALL);
		//删除searchable相关key
		redisManager.remove(key);
	}
	
	@After(value = "deleteRowAndNameCachePointCut(m)")
	public void deleteRowAndNameCacheAdvice(JoinPoint jp, Object m) throws Throwable {
		// 前置检查
		Class mclazz = m.getClass();
		if(!mclazz.isAnnotationPresent(BaseDataCache.class)){
			// TODO: deleteByParams(Map) delete(id)
			return;
		}

		// 处理缓存
		String baseKey = this.getBaseKeyByEntityClass(mclazz);
		Object idValue = ReflectUtils.invokeGetter(m, "id");
		String no = String.valueOf(idValue);

		String rowKey = baseKey + keySplit + Fetches.ROW + no;
		String nameKey = baseKey + keySplit + Fetches.NAME + no;
		
		redisManager.remove(nameKey);
		redisManager.remove(rowKey);
	}
	
	private String getBaseKeyByEntityClass(Class<?> entityClass){
		String baseKey = entityClass.getSimpleName();
		return baseKey;
	}
}
